package com.ljs.myquartz.quartz.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljs.myquartz.common.annotation.Log;
import com.ljs.myquartz.common.core.domain.AjaxResult;
import com.ljs.myquartz.common.core.page.TableDataInfo;
import com.ljs.myquartz.common.enums.BusinessType;
import com.ljs.myquartz.common.utils.poi.ExcelUtil;
import com.ljs.myquartz.quartz.domain.SysJob;
import com.ljs.myquartz.quartz.service.ISysJobService;
import com.ljs.myquartz.quartz.util.JsonResult;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class index {
    @Autowired
    private ISysJobService jobService;
    @RequestMapping("/hh")
    public String HH(){
        return "hh";
    }
    @RequestMapping("/myjob")
    public String myjob(){
        return "/monitor/job/myjob";
    }
    @RequestMapping("/list")
    @ResponseBody
    public JsonResult  list(@RequestParam("page")int page, @RequestParam("limit")int limit,SysJob job)
    {
        System.out.println("job"+job);
        JsonResult jsonResult=new JsonResult();
        Page pages= PageHelper.startPage(page,limit);
        List<SysJob> list = jobService.selectJobList(job);
        PageInfo pageInfo=new PageInfo(list);
        System.out.println("总条数"+pageInfo.getTotal());
        jsonResult.setCount((int) pageInfo.getTotal());

        jsonResult.setData(list);
        return jsonResult;
    }
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)

    @RequestMapping ("/export")
    @ResponseBody
    public AjaxResult export(SysJob job)
    {
        List<SysJob> list = jobService.selectJobList(job);
        ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);

        return util.exportExcel(list, "定时任务");
    }





    @RequestMapping("/updstatus")
    @ResponseBody
    public AjaxResult changeStatus(@RequestParam("jobId") Long jobId,@RequestParam("status") String status) throws SchedulerException
    {
        AjaxResult ajaxResult=new AjaxResult();

        System.out.println("要更新的任务"+jobId);
        System.out.println("要更新的状态"+status);
        SysJob newJob = jobService.selectJobById(jobId);
        System.out.println("要更新"+newJob);
          if(status.equals("1")){
              status="0";
              System.out.println("1");
              System.out.println("换"+status);
          } else if(status.equals("0")){
            status="1";
        }
        System.out.println("最后status"+status);
        newJob.setStatus(status);
        System.out.println("新"+newJob);

     int i=   jobService.changeStatus(newJob);
     if(i>0){
         ajaxResult.success("状态修改成功");
     }

        return   ajaxResult;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public  AjaxResult delete(@RequestParam("jobId") String jobId) throws SchedulerException {
        System.out.println("要删除的任务"+jobId);
        jobService.deleteJobByIds(jobId);
        return AjaxResult.success("删除成功");

    }
    @RequestMapping("/toadd")
    public String toadd(){
        return "/monitor/job/add";
    }


}
